package ModuleFour.task04;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class TCPClientTest {

    public static void main(String[] args) {


        Socket s = null;
        PrintStream ps = null;
        BufferedInputStream bis  = null;
        BufferedOutputStream bos  = null;
        ObjectOutputStream oos = null;

        //将用户从键盘上输入的用户名和密码传给User类
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入UserMessage类型中的对象");
        String usermessage = sc.next();
        System.out.println("请输入用户名");
        String username = sc.next();
        System.out.println("请输入用户密码");
        int password = sc.nextInt();


        try {

            //UserMessage um = new UserMessage(usermessage, new User(username, password));
            User u1 = new User(username,password);


            //1、创建Socket类型的对象并提供主机名和端口号
            s = new Socket("172.29.19.102",8888);
            System.out.println("连接服务器成功！");
            //2、使用输入输出流进行通信
            //实现客户端向服务器发送
            oos = new ObjectOutputStream(s.getOutputStream());
            oos.writeObject(u1);
            System.out.println("客户端发送数据成功！");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //3、关闭Socket并释放有关资源

            if (null != ps) {
                ps.close();
            }
            if (null != s) {
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
